﻿namespace ClassyBlog.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;

    using ExtensionMethods;

    public static class DataFileConfiguration
    {
        private static readonly IEnumerable<char> restrictedCharacters =
            BuldRestrictedCharacters();

        private static readonly IEnumerable<string> defaultDateFormats =
            BuildFormats();

        private static readonly ICollection<string> dateFormats =
            new List<string>(defaultDateFormats);

        private static char separator = '_';
        private static char hiddenPrefix = '.';

        public static char Separator
        {
            [DebuggerStepThrough]
            get
            {
                return separator;
            }

            [DebuggerStepThrough]
            set
            {
                Validate(value);

                separator = value;
            }
        }

        public static char HiddenPrefix
        {
            [DebuggerStepThrough]
            get
            {
                return hiddenPrefix;
            }

            [DebuggerStepThrough]
            set
            {
                Validate(value);

                hiddenPrefix = value;
            }
        }

        public static ICollection<string> DateFormats
        {
            [DebuggerStepThrough]
            get
            {
                return dateFormats.Any() ?
                       dateFormats :
                       new List<string>(defaultDateFormats);
            }
        }

        private static void Validate(char value)
        {
            if (restrictedCharacters.Contains(value))
            {
                throw new ArgumentException(
                    TextMessages.DataFileConfiguration_InvalidCharacter.
                    Interpolate(
                    string.Join(", ", restrictedCharacters.ToArray())));
            }

            if (DateFormats.Any(dateFormat => dateFormat.Contains(value)))
            {
                throw new ArgumentException(
                    TextMessages.DataFileConfiguration_MatchingCharacter);
            }
        }

        private static IEnumerable<string> BuildFormats()
        {
            return new[]
                       {
                           "yyyy-MM-dd-HH-mm-ss",
                           "yyyy-MM-dd-HH-mm",
                           "yyyy-MM-dd-HH",
                           "yyyy-MM-dd",
                           "yyyyMMddHHmmss",
                           "yyyyMMddHHmm",
                           "yyyyMMddHH",
                           "yyyyMMdd"
                       };
        }

        private static IEnumerable<char> BuldRestrictedCharacters()
        {
            return Path.GetInvalidFileNameChars()
                       .Union(Path.GetInvalidPathChars())
                       .Distinct()
                       .ToList();
        }
    }
}